import pytz
from datetime import datetime, timedelta
import uuid
import logging

logger = logging.getLogger('user_data_util')


# 获取到Seq
def get_seq(index):
    if index.UserName is not None:
        head_img_url = index.HeadImgUrl
        seq_head = head_img_url.split("seq=")[1]
        return seq_head.split("&username")[0]


def get_today():
    tz = pytz.timezone('Asia/Shanghai')
    today = datetime.now(tz).strftime('%Y/%m/%d %H:%M:%S')
    logger.info("today wechat_user={}".format(today))
    return today

def get_five_minutes_ago():
    tz = pytz.timezone('Asia/Shanghai')
    # 获取当前时间
    now = datetime.now(tz)
    # 获取五分钟之前的时间
    five_minutes_ago = now - timedelta(minutes=2)
    # 格式化时间
    formatted_time = five_minutes_ago.strftime('%Y/%m/%d %H:%M:%S')
    # 记录日志
    logger.info("five minutes ago wechat_user={}".format(formatted_time))
    return formatted_time

def mul_value(member_list, nick_name):
    # 求和
    # 95%
    # 求平均值
    # 数量
    # 减去自己
    mul_num = len(member_list) - 1
    total = 0
    for index in member_list:
        if index.NickName == nick_name:
            continue
        seq = get_seq(index)
        total = total + int(seq)
    mul_avg = total / mul_num
    mul_value = 3 * mul_avg
    mul_value_low = total - mul_value
    mul_value_top = total + mul_value
    return mul_value_low, mul_value_top, total


def mul_attr_status(member_list):
    # 求和
    # 95%
    # 求平均值
    # 数量
    # 减去自己
    mul_num = len(member_list) - 1
    mul_ori = 0
    for index in member_list:
        attr_status = index.AttrStatus
        mul_ori = mul_ori + attr_status
    mul_avg = mul_ori / mul_num
    mul_value_low = mul_ori - mul_num * 0.80 * mul_avg
    mul_value_top = mul_ori + mul_num * 0.80 * mul_avg
    return mul_value_low, mul_value_top, mul_ori


# 将新的数据，通过seq作为key，数据作为map
def build_new_seq_map(member_list):
    new_seq_map = {}
    for index in member_list:
        # seq个人好友的唯一标识
        seq = get_seq(index)
        new_seq_map[seq] = index
    return new_seq_map


# 将用户信息，以seq为key，以朋友信息为value,组装为map
def get_wechat_map(wechat_user_value):
    wechat_map = {}
    for index in wechat_user_value:
        wechat_map[index[11]] = index
    return wechat_map


def generate_unique_id() -> str:
    """
    生成唯一的8位纯数字ID
    :return: 生成的ID
    """
    while True:
        unique_id = str(uuid.uuid4().int)[:8]
        if len(unique_id) == 8 and unique_id.isnumeric():
            return unique_id
